﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BrainTechLLC;

namespace UserInterface
{
	public partial class TypeAndMethodEditor : UserControl
	{
		protected Type _lastType = typeof(string);
		protected string _methodName;

		[Bindable(BindableSupport.Yes)]
		public string MethodName
		{
			get { return _methodName; }
			set
			{
				_methodName = value;
				if (DesignMode) return;
				UpdateBindings();
			}
		}

		[Bindable(BindableSupport.Yes)]
		public string TypeName
		{
			get { return typeEditor1.SelectedType; }
			set
			{
				if (DesignMode) return;
				typeEditor1.SelectedType = value;
			}
		}

		public void UpdateBindings()
		{
			if (_methodName != null)
			{
				Type t = TypeProvider.GetInstance().GetType(typeEditor1.SelectedType, false);

				if (t == _lastType)
					return;

				_lastType = t;

				cboMethods.Items.Clear();
				cboMethods.Items.Add(string.Empty);

				var methods = TypeProvider.GetInstance().FindMethods(t, null, null);
				List<string> methodNames = new List<string>();
				methods.ForEach((m) => methodNames.Add(m.Name));
				cboMethods.Items.AddRange(methodNames.ToArray());
				for (int n = 0; n < cboMethods.Items.Count; n++)
				{
					if (cboMethods.Items[n].ToString().IndexOf(_methodName, StringComparison.OrdinalIgnoreCase) == 0)
					{
						cboMethods.SelectedIndex = n;
						break;
					}
				}
			}
		}


		public TypeAndMethodEditor()
		{
			InitializeComponent();
		}
	}
}
